using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class NormalSlime : EnemyBase
{
    private TileTest mapManager;

    void Awake()
    {
        mapManager = FindObjectOfType<TileTest>();
    }

    private void Start()
    {
        InitEnemy();
        OnDeath += JudgmentGroundColor;
    }

    /// <summary>
    /// 判断脚下颜色
    /// </summary>
    private void JudgmentGroundColor()
    {
        var hit = Physics2D.Raycast(checkOrigin.position, (downCheckPoint - checkOrigin.position).normalized, 0.5f, LayerMask.GetMask("Ground"));
        if (hit.collider != null)
        {
            //需要一个获取tile数据的方法 参数是坐标，返回Tile数据
            Vector3Int pos;

            var tile = mapManager.Tilecolor(mapManager.map.WorldToCell(new Vector3(hit.point.x - 0.01f, hit.point.y - 0.01f, 0)),out pos);
            Debug.Log(tile.name);
            RemoveSameColor(tile,pos);
        }
    }


    /// <summary>
    /// 清除同样颜色的tile
    /// </summary>
    private void RemoveSameColor(TileBase tile,Vector3Int pos)
    {
        // List<Vector3Int> tilePositions = new List<Vector3Int>();
        // FoundTile(tilePositions, tileData.position, tileData);
        //FoundTile(tilePositions, pos, tile);
        // tilePositions.ForEach(item => { Debug.Log("消除tile:" +item); mapManager.map.SetTile(item,null);});
        mapManager.RemoveTile(pos, tile);
    }

    #region 自定义栈
    //private static int maxStackSize = 255;
    //private int[] xStack = new int[maxStackSize];
    //private int[] yStack = new int[maxStackSize];
    //private int stackSize;

    //private void EmptyStack()
    //{
    //    while (PopX() != -65535)
    //    {
    //        PopY();
    //    }
    //}

    //private void Push(int x,int y)
    //{
    //    stackSize++;
    //    if (stackSize == maxStackSize)
    //    {
    //        int[] newXStack = new int[maxStackSize * 2];
    //        int[] newYStack = new int[maxStackSize * 2];
    //        xStack.CopyTo(newXStack, 0);
    //        yStack.CopyTo(newYStack, 0);
    //        xStack = newXStack;
    //        yStack = newYStack;
    //    }
    //    xStack[stackSize - 1] = x;
    //    yStack[stackSize - 1] = y;
    //}

    //private int PopX()
    //{
    //    if (stackSize == 0)
    //    {
    //        return -65535;
    //    }
    //    else
    //    {
    //        return xStack[stackSize - 1];
    //    }
    //}

    //private int PopY()
    //{
    //    int value = yStack[stackSize - 1];
    //    stackSize--;
    //    return value;
    //}

    #endregion 

    
    
    /// <summary>
    /// 泛洪法递归
    /// </summary>
    //private void FoundTile(List<Vector3Int> positions,Vector3Int oldPosition, TileBase oldTile)
    //{
    //    var oldData = mapManager.GetTileData(oldTile);
    //    Vector3Int currentPos;
    //    var currentTile = mapManager.Tilecolor(oldPosition, out currentPos);
    //    var currentData = mapManager.GetTileData(currentTile);
    //    Debug.Log(currentData);
    //    if ( currentData!= null && currentData.data.color.id ==oldData.data.color.id && !currentData.isFind)
    //    {
    //        Debug.Log(currentData.data.name);
    //        //positions.Add(currentPos);
    //        mapManager.map.SetTile(oldPosition, null);
    //        //currentData.isFind = true;
    //        FoundTile(positions, new Vector3Int(currentPos.x + 1, currentPos.y, currentPos.z), oldTile); //右
    //        FoundTile(positions, new Vector3Int(currentPos.x - 1, currentPos.y, currentPos.z), oldTile); //左
    //        FoundTile(positions, new Vector3Int(currentPos.x, currentPos.y + 1, currentPos.z), oldTile); //上
    //        FoundTile(positions, new Vector3Int(currentPos.x, currentPos.y - 1, currentPos.z), oldTile); //下
    //    }
    //}


}
